﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AssemblyInfo.cs" company="" />
// <summary>
//   Related assembly information.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Utilities
{
    using System;
    using System.Globalization;
    using System.IO;
    using System.Reflection;

    /// <summary>
    /// Related assembly information.
    /// </summary>
    public static class AssemblyInfo
    {
        /// <summary>
        /// Get a stream representing an embedded object within a given containing assembly.
        /// </summary>
        /// <param name="containerAssembly">The assembly containing the required object.</param>
        /// <param name="embeddedObjectName">The name of the required embedded object.</param>
        /// <returns>A stream representing the required embedded object.</returns>
        public static Stream GetEmbeddedObjectStream(Assembly containerAssembly, string embeddedObjectName)
        {
            string assemblyName = containerAssembly.GetName().Name;
            string requiredObjectName = string.Join(".", assemblyName, embeddedObjectName);

            var resultStream = containerAssembly.GetManifestResourceStream(requiredObjectName);

            if (resultStream == null)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Embedded resource '{0}' does not exist.", requiredObjectName));
            }

            return resultStream;
        }
    }
}
